<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="对话主题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入对话主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否删除" prop="isDeleted">
        <el-select v-model="queryParams.isDeleted" placeholder="是否删除" clearable>
          <el-option
            v-for="dict in deleteOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['chat:conversation:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="conversationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会话ID" align="center" prop="conversationId" />
      <el-table-column label="对话主题" align="center" prop="title" :show-overflow-tooltip="true" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否删除" align="center" prop="isDeleted">
        <template slot-scope="scope">
          <dict-tag :options="deleteOptions" :value="scope.row.isDeleted"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewMessages(scope.row)"
            v-hasPermi="['chat:conversation:query']"
          >查看对话</el-button>
          <el-button
            v-if="scope.row.isDeleted === 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleSoftDelete(scope.row)"
            v-hasPermi="['chat:conversation:remove']"
          >删除</el-button>
          <el-button
            v-if="scope.row.isDeleted === 1"
            size="mini"
            type="text"
            icon="el-icon-refresh-left"
            @click="handleRestore(scope.row)"
            v-hasPermi="['chat:conversation:restore']"
          >恢复</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listConversation, getConversation, delConversation, restoreConversation } from "@/api/chat/conversation";

export default {
  name: "Conversation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 对话表格数据
      conversationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        userId: null,
        isDeleted: null
      },
      // 删除状态选项
      deleteOptions: [
        {
          value: "0",
          label: "未删除"
        },
        {
          value: "1",
          label: "已删除"
        }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询对话列表 */
    getList() {
      this.loading = true;
      listConversation(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        if (response.data && response.data.data) {
          this.conversationList = response.data.data;
          this.total = response.data.total || 0;
        } else {
          this.conversationList = [];
          this.total = 0;
        }
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        conversationId: null,
        title: null,
        userId: null,
        createTime: null,
        updateTime: null,
        isDeleted: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.conversationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看对话记录按钮操作 */
    handleViewMessages(row) {
      this.$router.push({ 
        path: '/chat/messages', 
        query: { 
          conversationId: row.conversationId,
          title: row.title,
          userId: row.userId
        }
      })
    },
    /** 单个删除按钮操作 */
    handleSoftDelete(row) {
      const conversationId = row.conversationId;
      this.$modal.confirm('是否确认删除对话编号为"' + conversationId + '"的对话？删除后用户将无法看到此对话。').then(function() {
        return delConversation(conversationId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 恢复删除的会话 */
    handleRestore(row) {
      const conversationId = row.conversationId;
      this.$modal.confirm('是否确认恢复对话编号为"' + conversationId + '"的对话？恢复后用户将可以看到此对话。').then(function() {
        return restoreConversation(conversationId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("恢复成功");
      }).catch(() => {});
    },
    /** 批量删除按钮操作 */
    handleDelete() {
      const conversationIds = this.ids;
      this.$modal.confirm('是否确认删除对话编号为"' + conversationIds + '"的对话？删除后用户将无法看到这些对话。').then(function() {
        return delConversation(conversationIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>